package com.suneast.suneaststore.modules.sort;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.suneast.suneaststore.R;
import com.suneast.suneaststore.modules.search.SearchFragment;
import com.suneast.suneaststore.modules.sort.adapter.LeftMenuAdapter;
import com.suneast.suneaststore.modules.sort.adapter.SortAdapter;
import com.suneast.suneaststore.bean.sort.ItemBean;
import com.suneast.suneaststore.bean.sort.SubCate;
import com.suneast.suneaststore.bean.sort.TopCate;
import com.suneast.suneaststore.modules.sort.event.SortItemEvent;
import com.suneast.suneaststore.modules.sort.event.SortPageEvent;
import com.suneast.suneaststore.ui.fragments.bottom.BottomItemFragment;
import com.suneast.suneaststore.ui.recycler.snaphelper.AlignTopSnapHelper;
import com.suneast.suneaststore.util.log.LatteLogger;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;

public class SortFragment extends BottomItemFragment implements SortAdapter.ItemClickListener {
    @Override
    public @LayoutRes
    int setLayout() {
        return R.layout.fragment_sort;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        EventBus.getDefault().register(this);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initView();
        SortRequestHandler.requestSortPageData();
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

    @BindView(R.id.rv_menu)
    RecyclerView menuRv = null;
    LeftMenuAdapter menuAdapter;


    @BindView(R.id.top_img)
    ImageView topImgV = null;

    @BindView(R.id.category_content_recycler_view)
    RecyclerView contentRV = null;

    SortAdapter adapter;

    private void initView() {
        contentRV.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new SortAdapter(R.layout.item_category);
        adapter.setItemClickListener(this);
        adapter.setOnItemChildClickListener((BaseQuickAdapter adapter, View view, int position) -> {
            if (view.getId() == R.id.tv_show_all_category) {
                ItemBean bean = (ItemBean) adapter.getData().get(position);
                SearchFragment delegate = new SearchFragment();
                Bundle bundle = new Bundle();
                bundle.putInt(SearchFragment.CATE, bean.getCurrent().getId());
                delegate.setArguments(bundle);
                getParentDelegate().start(delegate);
            }
        });
        contentRV.setAdapter(adapter);

        menuRv.setLayoutManager(new LinearLayoutManager(getContext()));
        menuAdapter = new LeftMenuAdapter(new ArrayList<>());
        menuAdapter.setOnItemClickListener((pos) -> {
            menuAdapter.updatePos(pos);
            SortRequestHandler.requestSortItemData(dataList.get(pos).getId(),
                    dataList.get(pos).getType());
        });

        menuRv.setAdapter(menuAdapter);
        new AlignTopSnapHelper().attachToRecyclerView(menuRv);
    }

    private ArrayList<TopCate> dataList;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefreshPage(SortPageEvent event) {
        dataList = event.getTopCateList();
        menuAdapter.updateDatas(dataList);
        Glide.with(getContext()).load(event.getImgUrl()).into(topImgV);
        adapter.replaceData(event.getItemBeanList());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefreshItem(SortItemEvent event) {
        adapter.replaceData(event.getItemBeanList());
    }

    @Override
    public void onItemClick(SubCate cate) {
        LatteLogger.d("debug", "cateId = " + cate.getId());
        Bundle bundle = new Bundle();
        if(cate.getType() == 1){ // cate的id 表示商品id
            bundle.putInt(SearchFragment.CATE, cate.getId());
        }else if(cate.getType() == 2){ // cate的id 表示品牌id
            bundle.putInt(SearchFragment.BRAND,cate.getId());
        }
        String filterStr = cate.getFilters();
        if (!"".equals(filterStr)) {
            bundle.putString(SearchFragment.FILTERS, filterStr);
        }
        SearchFragment delegate = new SearchFragment();
        delegate.setArguments(bundle);
        getParentDelegate().start(delegate);
    }
}
